package com.carkeeper.search.service.impl;


import com.carkeeper.common.utils.Result;
import com.carkeeper.pojo.ServiceInfo;
import com.carkeeper.search.dao.EsManagerMapper;
import com.carkeeper.search.service.EsManagerService;
import com.carkeeper.serviceManagement.feign.ServiceFeign;
import com.carkeeper.serviceManagement.feign.ServiceTypeFeign;
import com.carkeeper.serviceManagement.pojo.Service;
import com.carkeeper.serviceManagement.pojo.ServiceType;
import com.carkeeper.subject.feign.SubjectFeign;
import com.carkeeper.subject.pojo.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;


import java.util.ArrayList;
import java.util.List;

@org.springframework.stereotype.Service
public class EsManagerServiceImpl implements EsManagerService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private ServiceFeign serviceFeign;
    @Autowired
    private SubjectFeign subjectFeign;
    @Autowired
    private EsManagerMapper esManagerMapper;
    @Autowired
    private ServiceTypeFeign serviceTypeFeign;

    @Override
    public void createIndexAndMapping() {
        //创建索引
        elasticsearchTemplate.createIndex(ServiceInfo.class);
        //创建映射
        elasticsearchTemplate.putMapping(ServiceInfo.class);
    }

    //批量导入
    @Override
    public void importAll() {
        List<Service> serviceList = serviceFeign.findListById("all");
        if (serviceList == null || serviceList.size() < 0) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        List<ServiceInfo> serviceInfos = new ArrayList<>();
        //遍历添加serviceInfo
        for (Service service : serviceList) {
            ServiceInfo serviceInfo = createServiceInfo(service);
            serviceInfos.add(serviceInfo);
        }
        esManagerMapper.saveAll(serviceInfos);

        }




    @Override
    public void importDataToESByServiceId(Long id) {

    }

    @Override
    public void delDataByServiceId(Long id) {

    }

    //封装serviceInfo
    private ServiceInfo createServiceInfo(Service service) {
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setId(service.getId());//服务ID
        serviceInfo.setCode(service.getCode());//服务项目编码
        Result<Store> result = subjectFeign.findById(service.getStoreId());
        String name = result.getData().getName();
        serviceInfo.setStoreName(name);//门店名字
        serviceInfo.setServiceName(service.getName());//服务项目名称
        Result<ServiceType> serviceTypeResult = serviceTypeFeign.findById(service.getType_2());
        String serviceName = serviceTypeResult.getData().getName();
        serviceInfo.setServiceType(serviceName);//二级服务类型
        serviceInfo.setPrice(service.getNormalPrice());//价格
        serviceInfo.setStatus(service.getStatus());//状态
        return serviceInfo;


    }
}
